package com.pcbly.web.components.utils.template.pagemess;

import java.io.File;
import java.lang.reflect.Field;

import org.apache.commons.io.FileUtils;

public class ModelPageEdit extends ModelPage { 

	private String editCol = "";

	private String dateCol = "";

	public ModelPageEdit() {
		StringBuilder sb = new StringBuilder();
		sb
				.append("					<td ><html:text name=\"#{firstEntityL}.#{fieldName}\"> ");
		sb.append(" </html:text></td>\r\n");
		sb.append("					<td class=\"td-input\">");
		sb.append("<html:textfield dataType=\"#{dataType}\"  ");
		sb
				.append(" id=\"#{firstEntityL}.#{fieldName}\" name=\"#{firstEntityL}.#{fieldName}\">");
		sb.append("</html:textfield></td>\r\n");
		setEditCol(sb.toString());

		sb = new StringBuilder();
		sb.append("					<td ><html:text name=\"#{firstEntityL}");
		sb.append(".#{fieldName}\"></html:text></td>");
		sb.append("<td class=\"td-input\"><html:date id=\"birthDay\" ");
		sb
				.append(" name=\"#{firstEntityL}.#{fieldName}\" format=\"yyyy-MM-dd\" ");
		sb.append(" /><html:textfield dataType=\"date\"    ");
		sb.append(" id=\"#{firstEntityL}.#{fieldName}\"  ");
		sb.append(" name=\"#{firstEntityL}.#{fieldName}\"  ");
		sb.append(" value=\"%{birthDay}\" readonly=\"true\" ");
		sb.append(" />&nbsp;<img style=\"border:0px;\" ");
		sb.append("  src=\"<%=WEBPATH%>/images/calendar.gif\"  ");
		sb.append(" onclick=\"WdatePicker({dateFmt:'yyyy-MM-dd', ");
		sb.append(" el:'#{firstEntityL}.#{fieldName}'})\"></td>");
		setDateCol(sb.toString());

	} 

	public String getEditCol() {
		return editCol;
	}

	public void setEditCol(String editCol) {
		this.editCol = editCol;
	}

	public String getDateCol() {
		return dateCol;
	}

	public void setDateCol(String dateCol) {
		this.dateCol = dateCol;
	} 
	public void product() {
		try {
			File file = new File(getPagePath("edit.jsp"));
			String name = "page_edit.txt";
			StringBuilder sb = new StringBuilder();
			File fileXML = new File(model.getFilePath(packName, name));
			String mainTxt = model.readTpl(fileXML);
			String dataBody = productEditDataBody();
			mainTxt = mainTxt.replaceAll("\\#\\{dataBody\\}", dataBody);
			sb.append(mainTxt);
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());
			if (file != null) {
				file = null;
			}
			if (fileXML != null) {
				fileXML = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String productEditDataBody() {
		StringBuilder sb = new StringBuilder();
		int i = 0;
		sb.append("<tr>\r\n");
		for (Field field : fields) {
			if (field == null) {
				break;
			}
			if (i % 2 == 0 && i != 0) {
				sb.append("</tr>\r\n<tr>\r\n");
			}
			String typeName = field.getType().getName();
			typeName = typeName.toLowerCase();

			String col = "";
			String dataType = "string";
			if (typeName.indexOf("date") >= 0) {
				dataType = "date";
				col = dateCol.replaceAll("\\#\\{firstEntityL\\}", model
						.getFirstEntityL());
				col = col.replaceAll("\\#\\{fieldName\\}", field.getName());
			} else {
				if (typeName.indexOf("int") >= 0) {
					dataType = "int";
				} else if (typeName.indexOf("double") >= 0) {
					dataType = "double";
				} else if (typeName.indexOf("float") >= 0) {
					dataType = "float";
				} else if (typeName.indexOf("bool") >= 0) {
					dataType = "bool";
				}
				col = editCol.replaceAll("\\#\\{firstEntityL\\}", model
						.getFirstEntityL());
				col = col.replaceAll("\\#\\{fieldName\\}", field.getName());
			}

			col = col.replaceAll("\\#\\{dataType\\}", dataType);
			// #{dataType}
			sb.append(col);

			i++;
		}
		if (!(i % 2 == 0)) {
			sb.append("	<td colspan=\"2\">&nbsp;</td>\r\n");
			sb.append("</tr>\r\n");
		}
		return sb.toString();
	}
}
